<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<!doctype html>
<head>
<meta charset="UTF-8">
<meta name="viewport"
	content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title></title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<link rel="stylesheet" type="text/css"
	href="<c:url value="/static/jquery-easyui/themes/default/easyui.css"/>">
<link rel="stylesheet" type="text/css"
	href="<c:url value="/static/jquery-easyui/themes/icon.css"/>">
<script type="text/javascript"
	src="<c:url value="/static/jquery-easyui/jquery-1.6.min.js"/>"></script>
<script type="text/javascript"
	src="<c:url value="/static/jquery-easyui/jquery.easyui.min.js"/>"></script>
<script type="text/javascript"
	src="<c:url value="/static/jquery-easyui/plugins/jquery.panel.js"/>"></script>
<script type="text/javascript"
	src="<c:url value="/static/jquery-easyui/plugins/jquery.datagrid.js"/>"></script>
<script type="text/javascript"
	src="<c:url value="/static/jquery-easyui/plugins/jquery.messager.js"/>"></script>
<script type="text/javascript"
	src="<c:url value="/static/jquery-easyui/ext-for-framework.js.jsp"/>"></script>
<script type="text/javascript"
	src="<c:url value="/static/jquery-easyui/locale/easyui-lang-zh_CN.js"/>"></script>
<script type="text/javascript"
	src="<c:url value="/static/js/common.js"/>"></script>
<script>
	var Address = [];//下拉数组
	var addId = 1; //id 列的增加变量

	var AddressCost = [];//下拉数组
	var addIdCost = 1; //id 列的增加变量

	$.ajax({
		async : false,
		url : "<c:url value='/turnover/turnoverTypePullDown.do?space=0'/>",
		type : "post",
		data : {
			materType : 1
		},
		success : function(data) {
			Address = data;
			if (typeof (Address) == 'string') {
				Address = JSON.parse(Address);
			}
		}
	});

	$.ajax({
		async : false,
		url : "<c:url value='/cost/costTypePullDown.do?space=0'/>",
		type : "post",
		data : {
			materType : 1
		},
		success : function(data) {
			AddressCost = data;
			if (typeof (AddressCost) == 'string') {
				AddressCost = JSON.parse(AddressCost);
			}
		}
	});
	function initList(listDisplayEId) {
		$('#' + listDisplayEId).datagrid(
				{
					nowrap : false,
					striped : true,
					collapsible : true,
					remoteSort : true,
					notSelectByRow : false,
					onLoadError : function() {
						$.messager.alert('错误', '获取数据失败，请与管理员联系！', 'error');
					},
					columns : [ [ {
						field : 'turnoverSubId',
						title : 'id',
						width : 100,
						align : 'center',
						editor : {
							type : 'text',
							options : {
								required : true
							}
						},
						hidden : true
					}, {
						field : 'turnoverType',
						title : '营业种类',
						width : 100,
						align : 'center',
						editor : {
							type : 'combobox',
							options : {
								data : Address,
								valueField : "turnoverType",
								textField : "turnoverTypeName",
								editable : false
							}
						}
					}, {
						field : 'turnoverValue',
						title : '金额',
						width : 100,
						align : 'center',
						editor : {
							type : 'text',
							options : {
								required : true
							}
						}
					} ] ],
					toolbar : [
							{
								id : listDisplayEId + '_btn_add',
								text : '新增',
								iconCls : 'icon-add',
								handler : function() {

									$('#' + listDisplayEId).datagrid(
											'appendRow', {
												turnoverSubId : addId
											});
									addId = addId + 1;
									var lastIndex = $('#' + listDisplayEId)
											.datagrid('getRows').length - 1;
									$('#' + listDisplayEId).datagrid(
											'beginEdit', lastIndex);
								}
							},
							'-',
							{
								text : '撤销',
								iconCls : 'icon-undo',
								handler : function() {
									$('#' + listDisplayEId).datagrid(
											'rejectChanges');
									addId = 0;
								}
							} ]

				});
	};
	function initList2(listDisplayEId) {
		$('#' + listDisplayEId).datagrid(
				{
					nowrap : false,
					striped : true,
					collapsible : true,
					remoteSort : true,
					notSelectByRow : false,
					onLoadError : function() {
						$.messager.alert('错误', '获取数据失败，请与管理员联系！', 'error');
					},
					columns : [ [ {
						field : 'costSubId',
						title : 'id',
						width : 100,
						align : 'center',
						editor : {
							type : 'text',
							options : {
								required : true
							}
						},
						hidden : true
					}, {
						field : 'costType',
						title : '费用种类',
						width : 100,
						align : 'center',
						editor : {
							type : 'combobox',
							options : {
								data : AddressCost,
								valueField : "costType",
								textField : "costTypeName",
								editable : false
							}
						}
					}, {
						field : 'costValue',
						title : '金额',
						width : 100,
						align : 'center',
						editor : {
							type : 'text',
							options : {
								required : true
							}
						}
					}, {
						field : 'picture',
						title : '上传图片',
						formatter : formatFilebox,
						width : 150,
						align : 'center',
						editor : {
							type : 'file',
							options : {
								required : true
							}
						}
					} ] ],
					toolbar : [
							{
								id : listDisplayEId + '_btn_add',
								text : '新增',
								iconCls : 'icon-add',
								handler : function() {

									$('#' + listDisplayEId).datagrid(
											'appendRow', {
												costSubId : addIdCost
											});
									addIdCost = addIdCost + 1;
									var lastIndex = $('#' + listDisplayEId)
											.datagrid('getRows').length - 1;
									$('#' + listDisplayEId).datagrid(
											'beginEdit', lastIndex);
								}
							},
							'-',
							{
								text : '撤销',
								iconCls : 'icon-undo',
								handler : function() {
									$('#' + listDisplayEId).datagrid(
											'rejectChanges');
									addIdCost = 0;
								}
							} ]

				});
	};
	$(document).ready(function() {
		initList('listDisplay');
		initList2('listDisplay2');

	});

	function beginEditList(rows, list, id) {
		for (var i = 0; i < rows.length; i++) {
			var rowIndex = $(list).datagrid('getRowIndex', rows[i][id]);
			$(list).datagrid('beginEdit', rowIndex);
		}
	}
	//添加信息按钮响应函数
	function deal() {

		if ($('#date').datebox('getValue') == '') {
			$.messager.alert("提示", "请填写日期！", 'info');
			return;
		}

		var rows = $('#listDisplay').datagrid('getRows');
		var turnoverTypeArray = new Array();//校验种类唯一的数组
		for (var i = 0; i < rows.length; i++) {
			var rowIndex = $('#listDisplay').datagrid('getRowIndex',
					rows[i]['turnoverSubId']);
			$('#listDisplay').datagrid('endEdit', rowIndex);
			if (!isNumberAndNoZero(rows[i]['turnoverValue'])) {
				$.messager.alert("提示", "营业金额请填写数字！", 'info');
				beginEditList(rows, '#listDisplay', 'turnoverSubId');
				return;
			}
			if (rows[i]['turnoverType'] == '') {
				$.messager.alert("提示", "营业种类不能为空！", 'info');
				beginEditList(rows, '#listDisplay', 'turnoverSubId');
				return;
			}
			turnoverTypeArray[i] = rows[i]['turnoverType'];
		}
		//提前存入ajax from
		var form = new FormData();
		//费用模块
		var rowsCost = $('#listDisplay2').datagrid('getRows');
		var costTypeArray = new Array();//校验种类唯一的数组
		for (var i = 0; i < rowsCost.length; i++) {
			var rowIndexCost = $('#listDisplay2').datagrid('getRowIndex',
					rowsCost[i]['costSubId']);
			//提前存入表单 把文件
			var fileObj = document.getElementById("picture"
					+ rowsCost[i]['costSubId']).files[0];
			if (typeof (fileObj) != 'undefined') {
				// 获取文件对象
				form.append("picture" + rowsCost[i]['costSubId'], fileObj); // 文件对象
			}

			$('#listDisplay2').datagrid('endEdit', rowIndexCost);

			if (!isNumberAndNoZero(rowsCost[i]['costValue'])) {
				$.messager.alert("提示", "费用金额请填写数字！", 'info');
				beginEditList(rows, '#listDisplay', 'turnoverSubId');
				beginEditList(rowsCost, '#listDisplay2', 'costSubId');
				return;
			}
			if (rowsCost[i]['costType'] == '') {
				$.messager.alert("提示", "费用种类不能为空！", 'info');
				beginEditList(rows, '#listDisplay', 'turnoverSubId');
				beginEditList(rowsCost, '#listDisplay2', 'costSubId');
				return;
			}
			costTypeArray[i] = rowsCost[i]['costType'];
		}
		//校验数据
		if (rows.length == 0 && rowsCost.length == 0) {
			$.messager.alert("提示", "请添加营业种类和费用种类不能同时为空！", 'info');
			beginEditList(rows, '#listDisplay', 'turnoverSubId');
			beginEditList(rowsCost, '#listDisplay2', 'costSubId');
			return;
		}
		//校验是否有相同种类
		if (isRepeat(turnoverTypeArray)) {
			beginEditList(rows, '#listDisplay', 'turnoverSubId');
			beginEditList(rowsCost, '#listDisplay2', 'costSubId');
			$.messager.alert("提示", "有重复营业种类！", 'info');
			return;
		}
		//校验是否有相同种类
		if (isRepeat(costTypeArray)) {
			beginEditList(rows, '#listDisplay', 'turnoverSubId');
			beginEditList(rowsCost, '#listDisplay2', 'costSubId');
			$.messager.alert("提示", "有重复费用种类！", 'info');
			return;
		}

		//生成json数据
		var turnoverList = new Array();
		var jsonFater;

		for (var i = 0; i < rows.length; i++) {
			var jsonSun = "{turnoverType:'" + rows[i]['turnoverType']
					+ "',turnoverSubId:'" + rows[i]['turnoverSubId']
					+ "',turnoverValue:'" + rows[i]['turnoverValue'] + "'}";
			turnoverList.push(jsonSun);
		}
		jsonFater = JSON.stringify(turnoverList);
		$("#turnoverjson").attr("value", jsonFater);

		var costList = new Array();
		var jsonFaterCost;
		for (var i = 0; i < rowsCost.length; i++) {
			var jsonSunCost = "{costType:'" + rowsCost[i]['costType']
					+ "',costSubId:'" + rowsCost[i]['costSubId']
					+ "',costValue:'" + rowsCost[i]['costValue'] + "'}";
			costList.push(jsonSunCost);
		}
		jsonFaterCost = JSON.stringify(costList);
		$("#costjson").attr("value", jsonFaterCost);

		//ajax 提交表单数据
		form.append("author", "hooyes"); // 可以增加表单数据
		form.append("custName", document.getElementById('custName').value); // 可以增加表单数据
		form.append("date", $('#date').datebox('getValue')); // 可以增加表单数据
		form.append("costjson", jsonFaterCost); // 可以增加表单数据
		form.append("turnoverjson", jsonFater); // 可以增加表单数据
		form.append("custId", "${shopId }"); // 可以增加表单数据

		// XMLHttpRequest 对象
		var xhr = new XMLHttpRequest();
		xhr.open("post", '<c:url value='/financing/saveFinancing.do'/>', true);
		xhr.onload = function() {
			$.messager.alert('系统提示', '添加成功', 'info', function() {
				window.opener.window.tea();
				window.close();
			});
		};
		xhr.send(form);
	}

	//学习用函数 ajax 表单提交 html5
	function UpladFile() {
		var form = new FormData();
		form.append("costId", "2asd"); // 可以增加表单数据

		// XMLHttpRequest 对象
		var xhr = new XMLHttpRequest();
		xhr.open("post", '<c:url value='/financing/saveFinancingPicture.do'/>',
				true);
		xhr.onload = function() {
			/* 	$.messager.alert('系统提示', '添加成功', 'info', function() {
					window.opener.window.tea();
					window.close();
				}); */
		};
		xhr.send(form);
	}
	function formatFilebox(value, rowData, rowIndex) {
		var valueString = "<input type='file' accept='images/*' name='picture"+rowData['costSubId']+"' id='picture"+rowData['costSubId']+"' />";
		return valueString.toString();
	}
</script>
</head>

<body>
	<table width="100%">
		<tr></tr>
		<tr>
			<td>

				<div style="height: auto; background: #fafafa;width: 500px;padding: 2px;"
					class="easyui-panel" title="添加营业额信息" iconCls="icon-save"
					collapsible="true" minimizable="false" maximizable="false"
					closable="false">

					<form id="AddFoodForm" name="AddFoodForm" method="post"
						enctype="multipart/form-data" action="userlist.txt"
						onsubmit="return searchDGData(this.id,'listDisplay');">
						<table cellpadding="5">

							<tr>
								<td>客户名:</td>
								<td><input class="easyui-validatebox" type="text"
									id="custName" name="custName" required="true"
									value="${shopName }" readOnly="true" /></td>
							</tr>
							<tr>
								<td>日期:</td>
								<td><input class="easyui-datebox" type="text" id="date"
									name="date" required="date" editable="false" /></td>
							</tr>
							<tr>
								<td><input type="hidden" id="custId" name="custId"
									value="${shopId }" /></td>
							</tr>
							<tr>
								<td><input type="hidden" name="turnoverjson"
									id="turnoverjson" /></td>
							</tr>
							<tr>
								<td><input type="hidden" name="costjson" id="costjson" /></td>
							</tr>
						</table>
						<table cellpadding="5" width="400px">
							<tr>
								<td>
									<table width="100%" id="listDisplay" title="营业种类列表" iconCls="icon-list"
										singleSelect="true" idField="turnoverSubId"
										loadMsg="请稍候......" pagination="false" rownumbers="true">
										<thead>
											<tr>
												<th field="turnoverSubId" width="80" sortable="true"
													editor="text">id</th>
												<th field="turnoverType" width="80" sortable="true"
													editor="combobox">营业种类</th>
												<th field="turnoverValue" width="80" sortable="true"
													editor="text">金额</th>

											</tr>
										</thead>
									</table>
								</td>
							</tr>
						</table>
						<table cellpadding="5" width="400px">
							<tr>
								<td>
									<table width="100%" id="listDisplay2" title="费用种类列表" iconCls="icon-list"
										singleSelect="true" idField="costSubId" loadMsg="请稍候......"
										pagination="false" rownumbers="true">
										<thead>
											<tr>
												<th field="costSubId" width="80" sortable="true"
													editor="text">id</th>
												<th field="costType" width="80" sortable="true"
													editor="combobox">费用种类</th>
												<th field="costValue" width="80" sortable="true"
													editor="text">金额</th>
												<th field="test" width="80" sortable="true" editor="picture">上传图片</th>
											</tr>
										</thead>
									</table>
								</td>
							</tr>
						</table>
						<a class="easyui-linkbutton" onclick="deal()">添加</a>
					</form>
				</div>
			</td>
		</tr>
	</table>
</body>
</html>